﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using Kefir.Model.Basic;
using System.Data;

namespace Kefir.DataLayer.Basic
{
    public abstract class ContentContextBase<T> : DbContext, IBasicDataAbstractionLayer<T> where T : Content
    {
        public DbSet<T> Contents { get; set; }
        public DbSet<Article> Articles { get; set; }

        void IBasicDataAbstractionLayer<T>.AddContent(T content)
        {
            Contents.Add(content);
        }

        IEnumerable<T> IBasicDataAbstractionLayer<T>.GetContentSet(Func<T, bool> condition)
        {
            if (condition != null)
                return Contents.Where(condition);
            return Contents;
        }

        void IBasicDataAbstractionLayer<T>.DeleteContent(T content)
        {
            Contents.Remove(content);
        }

        int IBasicDataAbstractionLayer<T>.SaveChanges()
        {
            return this.SaveChanges();
        }


        void IBasicDataAbstractionLayer<T>.AddArticle(Article article)
        {
            Articles.Add(article);
        }


        void IBasicDataAbstractionLayer<T>.DeleteArticle(Article article)
        {
            Articles.Remove(article);
        }

        IEnumerable<Article> IBasicDataAbstractionLayer<T>.GetArticles(Func<Article, bool> condition)
        {
            if (condition != null)
                return Articles.Where(condition);
            return Articles;
        }

        void IBasicDataAbstractionLayer<T>.UpdateContent(T content)
        {
            this.Entry(content).State = EntityState.Modified;
        }

        void IBasicDataAbstractionLayer<T>.UpdateArticle(Article article)
        {
            this.Entry(article).State = EntityState.Modified;
        }
    }
}
